<?php

class Control_Model_Table_Message extends Zend_Db_Table_Abstract {

    protected $_name = 'message_admin';

    public function addMessage($name, $phone, $mail, $message) {
        $data = array(
            'name' => $name,
            'phone' => $phone,
            'mail' => $mail,
            'text' => $message
        );
        $this->insert($data);
    }

    public function getAll() {
        $id = (int) $id;
        $row = $this->fetchRow('id = ' . $id);
        if (!$row) {
            throw new Exception("Count not find row $id");
        }
        return $row->toArray();
    }

    public function getAllUnreaded() {
        $select = $this->select();
        $select->where('unread=?', '1')
                ->order('id DESC')
                ->limit('10');
        $Data = $this->_db->query($select)->fetchAll();
        if (!$Data) {
            
        } else {
            return $Data;
        };
    }

    public function getAllMessages() {
        $select = $this->select();
        $select->order('id DESC');
        $Data = $this->_db->query($select)->fetchAll();
        if (!$Data) {

        } else {
            return $Data;
        };
    }

    public function getAllUnreadedCount() {
        $select = $this->select();
        $select->where('unread=?', '1')
                ->order('id DESC');
        $Data = $this->_db->query($select)->fetchAll();
        if (!$Data) {
            
        } else {
            return $Data;
        };
    }

    public function allReaded() {
        $data = array(
            'unread' => '0'
        );
        $this->update($data, '1');
    }

    public function getDataID($id) {
        $id = (int) $id;
        $row = $this->fetchRow('id =' . $id);
        if (!$row) {
            //           throw new Exception("Count not find row $id");
        } else {
            return $row->toArray();
        }
    }

        public function deleteData($id)
    {
        $this->delete('id =' . (int)$id);
    }

}